#!/usr/bin/env python3
# coding:utf-8

import os
from flask import Flask, request, send_file

app = Flask(__name__)

@app.route('/')
def hello_world():
    web="""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
    <a href="start">start</a>
    <br/>
    <a href="stop">stop</a>
    <br/>
    <a href="log">log</a>
</body>
</html>
"""
    return web

@app.route('/start')
def start_programe():
    return 'start'

@app.route('/stop')
def stop_programe():
    return 'stop'

@app.route('/log')
def log_show():
    pic = os.listdir("pic")
    html="<p>pic"
    for file in pic:
        html+="<a href=\"/image/%s\">%s</a><br/>"%(file,file)
    print(pic)
    html+="</p>"
    return html


image_html=" \
<!DOCTYPE html> \
<html>\
<head>\
    <title>Display Image</title>\
</head>\
<body>\
    <h1>显示图片</h1>\
    < img src=\"{{ url_for('static', filename='%s') }}\" alt=\"%s>\"\
</body>\
</html>\
"

def get_binary_io(file):
    with open(file, 'rb') as file:
        bytes_io = io.BytesIO(file.read().encode())
    return bytes_io

@app.route('/image/<image>')
def get_image(image):
    # url = request.url
    file_path="pic/%s"%(image)
    print("file_path=%s, image=%s"%(file_path,image))
    
    # return send_file('pic/114.jpg', mimetype='image/jpeg')
    if os.path.exists(file_path):
        return send_file(file_path, mimetype='image/jpeg')

    return "no this image:%s"%(file_path)

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8080)